package org.zsqx.action;

import java.awt.geom.Area;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.zsqx.bean.Admin;
import org.zsqx.bean.Dstype;
import org.zsqx.bean.Linkman;
import org.zsqx.bean.LinkmanBean;
import org.zsqx.bean.LinkmanType;
import org.zsqx.bean.Qfr;
import org.zsqx.bean.Town;
import org.zsqx.bean.Zdz;

@Action(value = "/settingAction", results = {
		@Result(name = "page", location = "/${returnPage}"),
		@Result(name = "list", location = "/settingAction!list.action",params={"className","${className}","operationMessage","${operationMessage}"},type="redirect"),
		@Result(name = "input", location = "/error.jsp")
})

public class SettingAction extends BaseAction {
	private Area area;
	private Dstype dstype;
	private Linkman linkman;
	private LinkmanBean linkmanBean;
	private Qfr qfr;
	private Town town;
	private Zdz zdz;
	private Admin admin;
	private LinkmanType linkmanType;
	
	private Object bean;
	private List beanList;
	private String className;
	private String operationMessage;
	private String returnPage;
	private String actionName;
	private String otherBean;
	private String pageType;
	
	public String list() throws UnsupportedEncodingException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		if(null!=operationMessage){
			operationMessage=new String(operationMessage.getBytes("ISO8859_1"),"utf-8");
		}
		bean = Class.forName("org.zsqx.bean."+className.substring(0, 1).toUpperCase()+className.substring(1)).newInstance();
		pageBean = rd.getPageBean(currentPage, rd.getCount(bean), pageSize);
		rd.setPageBean(pageBean);
		beanList = rd.findByPage(bean);
		returnPage = "login/system/"+getListPage(className);
		return "page";
	}
	public String show(){
		pageType = "show";
		bean = getBean(className);
		bean = rd.findById(bean);
		returnPage = "login/system/"+getSinglePage(className);
		return "page";
	}
	public String addPage() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		pageType = "add";
		returnPage = "login/system/"+getSinglePage(className);
		actionName = "settingAction!add.action";
		if(null!=otherBean)theOtherList(otherBean);
		return "page";
	}
	public String add(){
		bean = getBean(className);
		int id = rd.save(bean);
		if(id!=0){
			operationMessage = SUCCESS;
		}else{
			operationMessage = FAIL;
		}
		if(className.equals("linkman"))className="linkmanBean";
		return "list";
	}
	public String updatePage() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		pageType = "update";
		bean = getBean(className);
		bean = rd.findById(bean);
		if(null!=otherBean)theOtherList(otherBean);
		returnPage = "login/system/"+getSinglePage(className);
		actionName = "settingAction!update.action";
		return "page";
	}
	public String update(){
		bean = getBean(className);
		int id = rd.update(bean);
		if(id!=0){
			operationMessage = SUCCESS;
		}else{
			operationMessage = FAIL;
		}
		if(className.equals("linkman"))className="linkmanBean";
		return "list";
	}
	public String delete(){
		bean = getBean(className);
		int de = rd.delete(bean);
		if(de!=0){
			operationMessage = SUCCESS;
		}else{
			operationMessage = FAIL;
		}
		if(className.equals("linkman"))className="linkmanBean";
		return "list";
	}
	private Object getBean(String className){
		Map<String, Object> classMap = new HashMap<String, Object>();
		classMap.put("area", area);
		classMap.put("dstype", dstype);
		classMap.put("linkman", linkman);
		classMap.put("qfr", qfr);
		classMap.put("town", town);
		classMap.put("zdz", zdz);
		classMap.put("admin", admin);
		classMap.put("linkmanType", linkmanType);
		classMap.put("linkmanBean", linkmanBean);
		return classMap.get(className);
	}
	private String getListPage(String className){
		Map<String, String> pageMap = new HashMap<String, String>();
		pageMap.put("area", "areaSettingList.jsp");
		pageMap.put("dstype", "dsTypeSettingList.jsp");
		pageMap.put("linkman", "lmSettingList.jsp");
		pageMap.put("linkmanBean", "lmSettingList.jsp");
		pageMap.put("qfr", "qfrSettingList.jsp");
		pageMap.put("town", "townSettingList.jsp");
		pageMap.put("zdz", "zdzSettingList.jsp");
		pageMap.put("admin", "adminSettingList.jsp");
		pageMap.put("linkmanType","linkmanTypeSettingList.jsp");
		pageMap.put("linkmanBean","lmSettingList.jsp");
		return pageMap.get(className);
	}
	private String getSinglePage(String className){
		Map<String, String> pageMap = new HashMap<String, String>();
		pageMap.put("area", "areaSettingDetail.jsp");
		pageMap.put("dstype", "dsTypeSettingDetail.jsp");
		pageMap.put("linkman", "lmSettingDetail.jsp");
		pageMap.put("qfr", "qfrSettingDetail.jsp");
		pageMap.put("town", "townSettingDetail.jsp");
		pageMap.put("zdz", "zdzSettingDetail.jsp");
		pageMap.put("admin", "adminSettingDetail.jsp");
		pageMap.put("linkmanType", "linkmanTypeSettingDetail.jsp");
		pageMap.put("linkmanBean", "lmSettingDetail.jsp");
		return pageMap.get(className);
	}
	private void theOtherList(String otherBean) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		String[] other = otherBean.split(",");
		for (String string : other) {
			Object otherObject = Class.forName("org.zsqx.bean."+string.substring(0, 1).toUpperCase()+string.substring(1)).newInstance();
			List otherList = rd.find(otherObject);
			request.setAttribute(string+"List", otherList);
		}
	}
	public Area getArea() {
		return area;
	}
	public void setArea(Area area) {
		this.area = area;
	}
	public Dstype getDstype() {
		return dstype;
	}
	public void setDstype(Dstype dstype) {
		this.dstype = dstype;
	}
	public Linkman getLinkman() {
		return linkman;
	}
	public void setLinkman(Linkman linkman) {
		this.linkman = linkman;
	}
	public Qfr getQfr() {
		return qfr;
	}
	public void setQfr(Qfr qfr) {
		this.qfr = qfr;
	}
	public Town getTown() {
		return town;
	}
	public void setTown(Town town) {
		this.town = town;
	}
	public Object getBean() {
		return bean;
	}
	public void setBean(Object bean) {
		this.bean = bean;
	}
	public List getBeanList() {
		return beanList;
	}
	public void setBeanList(List beanList) {
		this.beanList = beanList;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getOperationMessage() {
		return operationMessage;
	}
	public void setOperationMessage(String operationMessage) {
		this.operationMessage = operationMessage;
	}
	public String getReturnPage() {
		return returnPage;
	}
	public void setReturnPage(String returnPage) {
		this.returnPage = returnPage;
	}
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	public Zdz getZdz() {
		return zdz;
	}
	public void setZdz(Zdz zdz) {
		this.zdz = zdz;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public String getOtherBean() {
		return otherBean;
	}
	public void setOtherBean(String otherBean) {
		this.otherBean = otherBean;
	}
	public LinkmanType getLinkmanType() {
		return linkmanType;
	}
	public void setLinkmanType(LinkmanType linkmanType) {
		this.linkmanType = linkmanType;
	}
	public String getPageType() {
		return pageType;
	}
	public void setPageType(String pageType) {
		this.pageType = pageType;
	}
	public LinkmanBean getLinkmanBean() {
		return linkmanBean;
	}
	public void setLinkmanBean(LinkmanBean linkmanBean) {
		this.linkmanBean = linkmanBean;
	}
	
}
